package com.alog.yzd.bookmall.web.servlet.book;

import com.alog.yzd.bookmall.domain.book.Book;
import com.alog.yzd.bookmall.pager.PageBaen;
import com.alog.yzd.bookmall.service.book.BookService;
import com.alog.yzd.bookmall.utils.WebUtils;
import com.alog.yzd.commons.CommonUtils;
import com.alog.yzd.commons.Print;
import com.alog.yzd.commons.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookService();
    /**
     * 根据书ID获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Book book = bookService.load(bid);
        req.setAttribute("book", book);
        Print.print("findByBid");
        return "f:/jsps/book/desc.jsp";
    }

    /**
     * 根据书名获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByCombination(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        Book book = CommonUtils.toBean(req.getParameterMap(), Book.class);
        PageBaen<Book> pb = bookService.findByCombination(book, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByCombination, pb:" + pb);
        return "f:/jsps/book/list.jsp";
    }
    /**
     * 根据书名获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByBname(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String bname = req.getParameter("bname");
        PageBaen<Book> pb = bookService.findByBname(bname, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByBname, pb:" + pb);
        return "f:/jsps/book/list.jsp";
    }
    /**
     * 根据出版衬获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByPress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String press = req.getParameter("press");
        PageBaen<Book> pb = bookService.findByPress(press, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByPress, pb:" + pb);
        return "f:/jsps/book/list.jsp";
    }

    /**
     * 根据作者获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String author = req.getParameter("author");
        PageBaen<Book> pb = bookService.findByAuthor(author, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByAuthor, pb:" + pb );
        Print.print("findByAuthor, author:" + author );
        return "f:/jsps/book/list.jsp";
    }



    /**
     * 根据分类获取书的列表
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findByCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        String cid = req.getParameter("cid");
        PageBaen<Book> pb = bookService.findByCatagory(cid, pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findByCategory, pb:" + pb);
        return "f:/jsps/book/list.jsp";
    }
}
